﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Web;

namespace MakeItWeb.Framework
{
    [Serializable]
    public class DailyWord : BusinessObject
    {
        private Guid m_guidDailywordId;
        private Guid m_guidHtmlPageId;
        private int m_iWeeknumId;
        private DateTime m_dtWeekStart;
        private DateTime m_dtWeekEnd;
        private string m_sAssignmentMondayTxt;
        private string m_sAssignmentTuesdayTxt;
        private string m_sAssignmentWednesdayTxt;
        private string m_sAssignmentThursdayTxt;
        private string m_sAssignmentFridayTxt;
        private string m_sAssignmentSaturdayTxt;
        private string m_sAssignmentSundayTxt;
        private bool m_bPublishedInd = false;
        public List<DailyWordQuestion> m_lstQuestions = null;
        public DailyWordStatus m_dailywordStatus = null;

        private DailyWord() {}

        public DailyWord(Guid guidHtmlPageId)
        {
            m_guidDailywordId = Guid.NewGuid();
            m_guidHtmlPageId = guidHtmlPageId;
        }

        #region Public properties

        public Guid DailywordId
        {
            get { return m_guidDailywordId; }
        }

        public Guid HtmlPageId
        {
            get { return m_guidHtmlPageId; }
        }

        public int WeeknumId
        {
            get { return m_iWeeknumId; }
            set
            {
                if (!value.Equals(m_iWeeknumId))
                {
                    m_iWeeknumId = value;
                    SetDirty();
                }
            }
        }

        public DateTime WeekStart
        {
            get { return m_dtWeekStart; }
            set
            {
                if (!value.Equals(m_dtWeekStart))
                {
                    m_dtWeekStart = value;
                    SetDirty();
                }
            }
        }

        public DateTime WeekEnd
        {
            get { return m_dtWeekEnd; }
            set
            {
                if (!value.Equals(m_dtWeekEnd))
                {
                    m_dtWeekEnd = value;
                    SetDirty();
                }
            }
        }

        public string AssignmentMondayTxt
        {
            get { return m_sAssignmentMondayTxt; }
            set
            {
                if (!value.Equals(m_sAssignmentMondayTxt))
                {
                    m_sAssignmentMondayTxt = value;
                    SetDirty();
                }
            }
        }

        public string AssignmentTuesdayTxt
        {
            get { return m_sAssignmentTuesdayTxt; }
            set
            {
                if (!value.Equals(m_sAssignmentTuesdayTxt))
                {
                    m_sAssignmentTuesdayTxt = value;
                    SetDirty();
                }
            }
        }

        public string AssignmentWednesdayTxt
        {
            get { return m_sAssignmentWednesdayTxt; }
            set
            {
                if (!value.Equals(m_sAssignmentWednesdayTxt))
                {
                    m_sAssignmentWednesdayTxt = value;
                    SetDirty();
                }
            }
        }

        public string AssignmentThursdayTxt
        {
            get { return m_sAssignmentThursdayTxt; }
            set
            {
                if (!value.Equals(m_sAssignmentThursdayTxt))
                {
                    m_sAssignmentThursdayTxt = value;
                    SetDirty();
                }
            }
        }

        public string AssignmentFridayTxt
        {
            get { return m_sAssignmentFridayTxt; }
            set
            {
                if (!value.Equals(m_sAssignmentFridayTxt))
                {
                    m_sAssignmentFridayTxt = value;
                    SetDirty();
                }
            }
        }

        public string AssignmentSaturdayTxt
        {
            get { return m_sAssignmentSaturdayTxt; }
            set
            {
                if (!value.Equals(m_sAssignmentSaturdayTxt))
                {
                    m_sAssignmentSaturdayTxt = value;
                    SetDirty();
                }
            }
        }

        public string AssignmentSundayTxt
        {
            get { return m_sAssignmentSundayTxt; }
            set
            {
                if (!value.Equals(m_sAssignmentSundayTxt))
                {
                    m_sAssignmentSundayTxt = value;
                    SetDirty();
                }
            }
        }

        public bool PublishedInd
        {
            get { return m_bPublishedInd; }
            set
            {
                if (!value.Equals(m_bPublishedInd))
                {
                    m_bPublishedInd = value;
                    SetDirty();
                }
            }
        }

        public string Status
        {
            get { return m_bPublishedInd ? "Published" : "Draft"; }
        }

        public DailyWordQuestion[] Questions
        {
            get
            {
                if (m_lstQuestions == null)
                {
                    m_lstQuestions = new List<DailyWordQuestion>();
                    m_lstQuestions.AddRange(DailyWordQuestion.ListDailyWordQuestion(DailywordId));
                }
                return m_lstQuestions.ToArray();
            }
        }

        public DailyWordStatus UserStatus
        {
            get { return m_dailywordStatus; }
        }

        #endregion

        public override void Load(System.Data.DataRow dr)
        {
            m_guidDailywordId = (Guid)dr["dailyword_id"];
            m_guidHtmlPageId = (Guid)dr["html_page_id"];
            m_iWeeknumId = (int)dr["weeknum_id"];
            m_dtWeekStart = (DateTime)dr["week_start_dt"];
            m_dtWeekEnd = (DateTime)dr["week_end_dt"];
            m_bPublishedInd = (bool)dr["published_ind"];
            m_sAssignmentMondayTxt = (string)dr["assignment_monday_txt"];
            m_sAssignmentTuesdayTxt = (string)dr["assignment_tuesday_txt"];
            m_sAssignmentWednesdayTxt = (string)dr["assignment_wednesday_txt"];
            m_sAssignmentThursdayTxt = (string)dr["assignment_thursday_txt"];
            m_sAssignmentFridayTxt = (string)dr["assignment_friday_txt"];
            m_sAssignmentSaturdayTxt = (string)dr["assignment_saturday_txt"];
            m_sAssignmentSundayTxt = (string)dr["assignment_sunday_txt"];

            // load user's status if user is authenticated
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                m_dailywordStatus = DailyWordStatus.GetDailyWordStatus(DailywordId);
            }

            SetLoaded();
        }

        public override void Save()
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pDailywordId"] = m_guidDailywordId;
            args["pHtmlPageId"] = m_guidHtmlPageId;
            args["pWeeknumId"] = m_iWeeknumId;
            args["pWeekStartDt"] = m_dtWeekStart;
            args["pWeekEndDt"] = m_dtWeekEnd;
            args["pPublishedInd"] = m_bPublishedInd;
            args["pAssignmentMondayTxt"] = m_sAssignmentMondayTxt;
            args["pAssignmentTuesdayTxt"] = m_sAssignmentTuesdayTxt;
            args["pAssignmentWednesdayTxt"] = m_sAssignmentWednesdayTxt;
            args["pAssignmentThursdayTxt"] = m_sAssignmentThursdayTxt;
            args["pAssignmentFridayTxt"] = m_sAssignmentFridayTxt;
            args["pAssignmentSaturdayTxt"] = m_sAssignmentSaturdayTxt;
            args["pAssignmentSundayTxt"] = m_sAssignmentSundayTxt;
            GenericCud.ExecuteCud("proc_cm_SaveDailyWord", args);

            SetDirty(false);
        }

        public static DailyWord GetDailyWord(Guid guidDailywordId)
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pDailywordId"] = guidDailywordId;
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_GetDailyWord", args))
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    var dw = new DailyWord();
                    dw.Load(ds.Tables[0].Rows[0]);
                    return dw;
                }
            }
            return null;
        }

        public static DailyWord GetCurrentDailyWord(Guid guidHtmlPageId, bool bUseCache = true)
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pHtmlPageId"] = guidHtmlPageId;
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_GetCurrentDailyWord", args, bUseCache))
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    var dw = new DailyWord();
                    dw.Load(ds.Tables[0].Rows[0]);
                    return dw;
                }
            }
            return null;
        }

        public static DailyWord[] ListDailyWord(Guid guidHtmlPageId)
        {
            List<DailyWord> lst = new List<DailyWord>();
            StoredProcArgs args = new StoredProcArgs();
            args["pHtmlPageId"] = guidHtmlPageId;
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_ListDailyWord", args))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    var dw = new DailyWord();
                    dw.Load(dr);
                    lst.Add(dw);
                }
            }
            return lst.ToArray();
        }

        public static DailyWord[] ListPreviousDailyWord(Guid guidHtmlPageId)
        {
            List<DailyWord> lst = new List<DailyWord>();
            StoredProcArgs args = new StoredProcArgs();
            args["pHtmlPageId"] = guidHtmlPageId;
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_ListPreviousDailyWord", args))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    var dw = new DailyWord();
                    dw.Load(dr);
                    lst.Add(dw);
                }
            }
            return lst.ToArray();
        }

        public static void DeleteDailyWord(Guid guidDailywordId)
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pDailyWordId"] = guidDailywordId;
            GenericCud.ExecuteCud("proc_cm_DeleteDailyWord", args);
        }

        public void AddQuestion(int iQuestionNum, string txtQuestion, string txtChoice1, string txtChoice2, string txtChoice3, string txtChoice4, int iCorrectChoice)
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pDailyWordId"] = DailywordId;
            args["pQuestionNum"] = iQuestionNum;
            args["pQuestionTxt"] = txtQuestion;
            args["pAnswerTypeCd"] = "MultipleChoices";
            StringBuilder sb = new StringBuilder();
            sb.Append("<choices>");
            sb.AppendFormat("<choice{1}>{0}</choice>", txtChoice1, iCorrectChoice == 1 ? @" isAnswer=""true""" : string.Empty);
            sb.AppendFormat("<choice{1}>{0}</choice>", txtChoice2, iCorrectChoice == 2 ? @" isAnswer=""true""" : string.Empty);
            sb.AppendFormat("<choice{1}>{0}</choice>", txtChoice3, iCorrectChoice == 3 ? @" isAnswer=""true""" : string.Empty);
            sb.AppendFormat("<choice{1}>{0}</choice>", txtChoice4, iCorrectChoice == 4 ? @" isAnswer=""true""" : string.Empty);
            sb.Append("</choices>");
            args["pChoicesXml"] = sb.ToString();
            GenericCud.ExecuteCud("proc_cm_AddDailyWordQuestion", args);

            // this will force it to reload
            m_lstQuestions = null;
        }
    }

    [Serializable]
    public class DailyWordStatus : BusinessObject
    {
        private Guid m_guidDailywordStatusId;
        private Guid m_guidDailywordId;
        private Guid m_guidUserId;
        private bool m_bReadInd;
        private string m_sReflectionTxt;
        private bool m_bSubmittedInd;
        private DateTime m_dtCreatedDttm;
        private DateTime? m_dtSubmittedDttm;
        private int? m_iScoreNum;
        private int? m_iPerfectScoreNum;
        private List<DailyWordAnswer> m_lstAnswer = null;

        #region Public properties

        public Guid DailywordStatusId
        {
            get { return m_guidDailywordStatusId; }
            private set { m_guidDailywordStatusId = value; }
        }

        public Guid DailywordId
        {
            get { return m_guidDailywordId; }
            private set { m_guidDailywordId = value; }
        }

        public Guid UserId
        {
            get { return m_guidUserId; }
            private set { m_guidUserId = value; }
        }

        public bool ReadInd
        {
            get { return m_bReadInd; }
            set
            {
                if (!value.Equals(m_bReadInd))
                {
                    m_bReadInd = value;
                    SetDirty();
                }
            }
        }

        public string ReflectionTxt
        {
            get { return m_sReflectionTxt; }
            set
            {
                if (!value.Equals(m_sReflectionTxt))
                {
                    m_sReflectionTxt = value;
                    SetDirty();
                }
            }
        }

        public bool SubmittedInd
        {
            get { return m_bSubmittedInd; }
            set
            {
                if (!value.Equals(m_bSubmittedInd))
                {
                    m_bSubmittedInd = value;
                    SetDirty();
                }
            }
        }

        public DateTime CreatedDttm
        {
            get { return m_dtCreatedDttm; }
            set
            {
                if (!value.Equals(m_dtCreatedDttm))
                {
                    m_dtCreatedDttm = value;
                    SetDirty();
                }
            }
        }

        public DateTime? SubmittedDttm
        {
            get { return m_dtSubmittedDttm; }
            set
            {
                if (!value.Equals(m_dtSubmittedDttm))
                {
                    m_dtSubmittedDttm = value;
                    SetDirty();
                }
            }
        }

        public int? ScoreNum
        {
            get { return m_iScoreNum; }
            set 
            {
                if (!value.Equals(m_iScoreNum))
                {
                    m_iScoreNum = value;
                    SetDirty();
                }
            }
        }

        public int? PerfectScoreNum
        {
            get { return m_iPerfectScoreNum; }
            set
            {
                if (!value.Equals(m_iPerfectScoreNum))
                {
                    m_iPerfectScoreNum = value;
                    SetDirty();
                }
            }
        }

        public DailyWordAnswer[] Answers
        {
            get 
            {
                if (m_lstAnswer == null)
                {
                    m_lstAnswer = new List<DailyWordAnswer>();
                    m_lstAnswer.AddRange(DailyWordAnswer.GetDailywordAnswer(DailywordStatusId, false));
                }
                return m_lstAnswer.ToArray(); 
            }
        }

        #endregion

        public override void Load(DataRow dr)
        {
            m_guidDailywordStatusId = (Guid)dr["dailyword_status_id"];
            m_guidDailywordId = (Guid)dr["dailyword_id"];
            m_guidUserId = (Guid)dr["UserId"];
            m_bReadInd = (bool)dr["read_ind"];
            if (!dr.IsNull("reflection_txt"))
                m_sReflectionTxt = (string)dr["reflection_txt"];
            m_bSubmittedInd = (bool)dr["submitted_ind"];
            m_dtCreatedDttm = (DateTime)dr["created_dttm"];
            if (!dr.IsNull("submitted_dttm"))
                m_dtSubmittedDttm = (DateTime)dr["submitted_dttm"];
            if (!dr.IsNull("score_num"))
                m_iScoreNum = (int)dr["score_num"];
            if (!dr.IsNull("perfect_score_num"))
                m_iPerfectScoreNum = (int)dr["perfect_score_num"];

            SetLoaded();
        }

        public override void Save()
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pDailywordStatusId"] = m_guidDailywordStatusId;
            args["pDailywordId"] = m_guidDailywordId;
            args["pUserId"] = m_guidUserId;
            args["pReadInd"] = m_bReadInd;
            if (!string.IsNullOrEmpty(m_sReflectionTxt))
                args["pReflectionTxt"] = m_sReflectionTxt;
            args["pSubmittedInd"] = m_bSubmittedInd;
            if (m_dtSubmittedDttm.HasValue)
                args["pSubmittedDttm"] = m_dtSubmittedDttm.Value;
            if (m_iScoreNum.HasValue)
                args["pScoreNum"] = m_iScoreNum.Value;
            if (m_iPerfectScoreNum.HasValue)
                args["pPerfectScoreNum"] = m_iPerfectScoreNum.Value;
            GenericCud.ExecuteCud("proc_cm_SaveDailyWordStatus", args);

            SetDirty(false);
        }

        public static DailyWordStatus GetDailyWordStatus(Guid guidDailyWordId)
        {
            var status = new DailyWordStatus();
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                return status;

            StoredProcArgs args = new StoredProcArgs();
            args["pDailyWordId"] = guidDailyWordId;
            args["pUserName"] = HttpContext.Current.User.Identity.Name;
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_GetDailyWordStatus", args))
            {
                if (ds.Tables[0].Rows.Count > 0)
                    status.Load(ds.Tables[0].Rows[0]);

                foreach (DataRow dr in ds.Tables[1].Rows)
                    status.AddAnswer(dr);
            }
            return status;
        }

        private void AddAnswer(DataRow dr)
        {
            if (m_lstAnswer == null)
                m_lstAnswer = new List<DailyWordAnswer>();

            var answer = new DailyWordAnswer();
            answer.Load(dr);
            m_lstAnswer.Add(answer);
        }

        public void Reset()
        {
            m_lstAnswer = null;
        }
    }

    [Serializable]
    public class DailyWordAnswer : BusinessObject
    {
        private Guid m_guidDailywordAnswerId;
        private Guid m_guidDailywordQuestionId;
        private Guid m_guidDailywordStatusId;
	    private string m_sAnswerTxt;
	    private bool m_bCorrectInd;
	    private DateTime m_dtCreatedDttm;
        private DateTime m_dtUpdatedDttm;

        public DailyWordAnswer()
        {
            m_guidDailywordAnswerId = Guid.NewGuid();
        }

        public DailyWordAnswer(Guid guidDailywordQuestionId, Guid guidDailywordStatusId)
            : this()
        {
            m_guidDailywordQuestionId = guidDailywordQuestionId;
            m_guidDailywordStatusId = guidDailywordStatusId;
        }

        #region Public properties

        public Guid DailywordAnswerId
        {
            get { return m_guidDailywordAnswerId; }
        }

        public Guid DailywordQuestionId
        {
            get { return m_guidDailywordQuestionId; }
        }

        public Guid DailywordStatusId
        {
            get { return m_guidDailywordStatusId; }
        }

        public string AnswerTxt
        {
            get { return m_sAnswerTxt; }
            set
            {
                if (!value.Equals(m_sAnswerTxt))
                {
                    m_sAnswerTxt = value;
                    SetDirty();
                }
            }
        }

        public bool CorrectInd
        {
            get { return m_bCorrectInd; }
            set
            {
                if (!value.Equals(m_bCorrectInd))
                {
                    m_bCorrectInd = value;
                    SetDirty();
                }
            }
        }

        public DateTime CreatedDttm
        {
            get { return m_dtCreatedDttm; }
        }

        public DateTime UpdatedDttm
        {
            get { return m_dtUpdatedDttm; }
        }

        #endregion

        public override void Load(DataRow dr)
        {
            m_guidDailywordAnswerId = (Guid)dr["dailyword_answer_id"];
            m_guidDailywordQuestionId = (Guid)dr["dailyword_question_id"];
            m_guidDailywordStatusId = (Guid)dr["dailyword_status_id"];
            m_sAnswerTxt = (string)dr["answer_txt"];
            m_bCorrectInd = (bool)dr["correct_ind"];
            m_dtCreatedDttm = (DateTime)dr["created_dttm"];
            m_dtUpdatedDttm = (DateTime)dr["updated_dttm"];

            SetLoaded();
        }

        public override void Save()
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pDailywordAnswerId"] = m_guidDailywordAnswerId;
            args["pDailywordQuestionId"] = m_guidDailywordQuestionId;
            args["pDailywordStatusId"] = m_guidDailywordStatusId;
            args["pAnswerTxt"] = m_sAnswerTxt;
            args["pCorrectInd"] = m_bCorrectInd;
            GenericCud.ExecuteCud("proc_cm_SaveDailywordAnswer", args);

            SetDirty(false);
        }

        internal static DailyWordAnswer[] GetDailywordAnswer(Guid guidDailywordStatusId, bool bUseCache = true)
        {
            List<DailyWordAnswer> lst = new List<DailyWordAnswer>();
            StoredProcArgs args = new StoredProcArgs();
            args["pDailywordStatusId"] = guidDailywordStatusId;
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_ListDailywordAnwer", args, bUseCache))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    DailyWordAnswer ans = new DailyWordAnswer();
                    ans.Load(dr);
                    lst.Add(ans);
                }
            }
            return lst.ToArray();
        }
    }

    [Serializable]
    public enum DailyWordQuestionType
    {
        MultipleChoices
    }

    [Serializable]
    public abstract class DailyWordQuestion : BusinessObject
    {
        public abstract bool IsCorrectAnswer(object answer);
        public abstract void LoadAnswer(string sAnswerTxt);
        public abstract string SaveAnswer();
        public abstract string AnswerForAdminView { get; }
        public abstract object CorrectAnswer { get; }

        private Guid m_guidDailywordQuestionId;
        private Guid m_guidDailywordId;
        private int m_iQuestionNum;
        private string m_sQuestionTxt;
        private DailyWordQuestionType m_enumAnswerType;
        private string m_sAnswerTxt;

        #region Public properties

        public Guid DailywordQuestionId
        {
            get { return m_guidDailywordQuestionId; }
        }

        public Guid DailywordId
        {
            get { return m_guidDailywordId; }
        }

        public int QuestionNum
        {
            get { return m_iQuestionNum; }
            set
            {
                if (!value.Equals(m_iQuestionNum))
                {
                    m_iQuestionNum = value;
                    SetDirty();
                }
            }
        }

        public virtual string QuestionTxt
        {
            get { return m_sQuestionTxt; }
            set
            {
                if (!value.Equals(m_sQuestionTxt))
                {
                    m_sQuestionTxt = value;
                    SetDirty();
                }
            }
        }

        public DailyWordQuestionType AnswerType
        {
            get { return m_enumAnswerType; }
            set
            {
                if (!value.Equals(m_enumAnswerType))
                {
                    m_enumAnswerType = value;
                    SetDirty();
                }
            }
        }

        public string AnswerTxt
        {
            get { return m_sAnswerTxt; }
            set
            {
                if (!value.Equals(m_sAnswerTxt))
                {
                    m_sAnswerTxt = value;
                    SetDirty();
                }
            }
        }

        #endregion

        public override void Load(DataRow dr)
        {
            m_guidDailywordQuestionId = (Guid)dr["dailyword_question_id"];
            m_guidDailywordId = (Guid)dr["dailyword_id"];
            m_iQuestionNum = (int)dr["question_num"];
            m_sQuestionTxt = (string)dr["question_txt"];
            m_enumAnswerType = (DailyWordQuestionType)Enum.Parse(typeof(DailyWordQuestionType), (string)dr["answer_type_cd"]);
            m_sAnswerTxt = (string)dr["answer_txt"];
            LoadAnswer(m_sAnswerTxt);

            SetLoaded();
        }

        public override void Save()
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pDailywordQuestionId"] = DailywordQuestionId;
            args["pDailywordId"] = DailywordId;
            args["pQuestionNum"] = QuestionNum;
            args["pQuestionTxt"] = QuestionTxt;
            args["pAnswerTypeCd"] = AnswerType.ToString();
            args["pAnswerTxt"] = SaveAnswer();
            GenericCud.ExecuteCud("proc_cm_SaveDailyWordQuestion", args);
        }

        internal static IEnumerable<DailyWordQuestion> ListDailyWordQuestion(Guid guidDailywordId)
        {
            List<DailyWordQuestion> lst = new List<DailyWordQuestion>();
            StoredProcArgs args = new StoredProcArgs();
            args["pDailyWordId"] = guidDailywordId;
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_ListDailyWordQuestion", args))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    DailyWordQuestion q = DailyWordQuestion.LoadQuestion(dr);
                    lst.Add(q);
                }
            }
            return lst.ToArray();
        }

        private static DailyWordQuestion LoadQuestion(DataRow dr)
        {
            var qType = (DailyWordQuestionType)Enum.Parse(typeof(DailyWordQuestionType), (string)dr["answer_type_cd"]);
            if (qType == DailyWordQuestionType.MultipleChoices)
            {
                DailyWordMultipleChoicesQuestion q = new DailyWordMultipleChoicesQuestion();
                q.Load(dr);
                return q;
            }
            else
                throw new NotImplementedException();
        }

        public static void DeleteDailywordQuestion(Guid guidDailywordQuestionId)
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pDailywordQuestionId"] = guidDailywordQuestionId;
            GenericCud.ExecuteCud("proc_cm_DeleteDailywordQuestion", args);
        }
    }

    [Serializable]
    public class DailyWordMultipleChoicesQuestion : DailyWordQuestion
    {
        private List<string> _lstChoice = new List<string>();
        private int _correctAnswer = -1;

        public override bool IsCorrectAnswer(object answer)
        {
            return (int)answer == _correctAnswer;
        }

        public override void LoadAnswer(string sAnswerTxt)
        {
            var doc = new XmlDocument();
            doc.LoadXml(sAnswerTxt);
            foreach (XmlNode node in doc.DocumentElement.SelectNodes("//choice"))
            {
                _lstChoice.Add(node.InnerText);
                if (node.Attributes != null && node.Attributes.Count > 0 && node.Attributes["isAnswer"].Value == "true")
                    _correctAnswer = _lstChoice.Count;
            }
        }

        public override string SaveAnswer()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<choices>");
            for (int i = 0; i < _lstChoice.Count; i++)
                sb.AppendFormat("<choice{1}>{0}</choice>", _lstChoice[i], IsCorrectAnswer(i+1) ? @" isAnswer=""true""" : string.Empty);
            sb.Append("</choices>");
            return sb.ToString();
        }

        public override string AnswerForAdminView
        {
            get 
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < _lstChoice.Count; i++)
                    sb.AppendFormat("<div{1}>({2}). {0}</div>", _lstChoice[i], IsCorrectAnswer(i + 1) ? @" style=""font-weight: bold""" : string.Empty, i+1);
                return sb.ToString();
            }
        }

        public DataTable Choices
        {
            get
            {
                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("ChoiceNum", typeof(int)));
                dt.Columns.Add(new DataColumn("QuestionGroupName", typeof(string)));
                dt.Columns.Add(new DataColumn("ChoiceTxt", typeof(string)));
                dt.Columns.Add(new DataColumn("IsAnswer", typeof(bool)));
                for (int i = 0; i < _lstChoice.Count; i++)
                {
                    var newRow = dt.NewRow();
                    newRow["ChoiceNum"] = i + 1;
                    newRow["QuestionGroupName"] = string.Format("Q_{0}", DailywordQuestionId);
                    newRow["ChoiceTxt"] = _lstChoice[i];
                    newRow["IsAnswer"] = IsCorrectAnswer(i + 1);
                    dt.Rows.Add(newRow);
                }
                return dt;
            }
        }

        public override object CorrectAnswer
        {
            get { return _correctAnswer; }
        }
    }
}
